package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.QuestionOptionAdp;
import com.baihan.angus.app.game.common.bean.response.ScriptStepResponse;

import java.util.List;

public class QuestionDialog extends Dialog {

    private Context mContext;
    private Callback mCallback;
    private WindowManager.LayoutParams mLayoutParams;
    private LinearLayout llClose;
    private RecyclerView rvOptions;

    private QuestionOptionAdp mQuestionOptionAdp;
    private List<ScriptStepResponse.DataX.Question> questionList;

    public QuestionDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = AppUtil.getScreenHeight(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 30);
        dialogWindow.getDecorView().setBackgroundResource(R.color.dialog_bg);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_question, (ViewGroup) null);
        setContentView(dialogView);
        rvOptions = dialogView.findViewById(R.id.rvOptions);
        llClose = dialogView.findViewById(R.id.llClose);

        llClose.setOnClickListener(v -> {
            cancel();
        });
    }

    public void initData(List<ScriptStepResponse.DataX.Question> questionList) {
        this.questionList = questionList;

        mQuestionOptionAdp = new QuestionOptionAdp(mContext, questionList.get(0).getData());
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false) {
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }
        };
        rvOptions.setLayoutManager(layoutManager);
        mQuestionOptionAdp.bindRecycleView(rvOptions);

        mQuestionOptionAdp.setOnItemClickListener(new QuestionOptionAdp.OnItemClickListener() {
            @Override
            public void onPrevious(int position) {
                rvOptions.scrollToPosition(position - 1);
                mQuestionOptionAdp.notifyDataSetChanged();
            }

            @Override
            public void onNext(int position) {
                rvOptions.scrollToPosition(position + 1);
                mQuestionOptionAdp.notifyDataSetChanged();
            }

            @Override
            public void onSubmit() {
//                Tooast.normalInfo("投票完成\n全部用户投票后公布结果");
                if (null != mCallback) {
                    mCallback.showMyAnswer(questionList.get(0).getData());
                }
                cancel();
            }
        });

//        mQuestionOptionAdp.updateQuestionList(questionList.get(0).getData());

    }

    public interface Callback {
        void showMyAnswer(List<ScriptStepResponse.DataX.Question.Data> data);
    }

}
